package jobs;

import dao.common.OrderManager;
import dao.common.UserManager;
import dao.hibernate.OrderManagerHB;
import dao.hibernate.UserManagerHB;
import models.exceptions.GetEntityException;
import models.shop.Order;
import models.shop.User;
import notifiers.Mails;
import play.jobs.Every;
import play.jobs.Job;

import java.io.IOException;
import java.util.List;

@Every("1h")
public class UncofirmedOrdersMailer extends Job{
    public void doJob() throws IOException, GetEntityException {
        OrderManager manager = new OrderManagerHB();
        List<Order> unconfirmed = manager.getUnconfirmed();
        if (unconfirmed.size() > 0){
            for(Order order : unconfirmed)
                sendEmail(order);
        }
    }

    private void sendEmail(Order order) throws IOException, GetEntityException {
        UserManager userManager = new UserManagerHB();
        List<User> admins = userManager.getAdmins();
        Mails.confirmOrderForAdmin(order.id, order.userInfo, admins);
    }
}
